<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="common/include :: header('新增图片')"/>
    <th:block th:include="common/include :: select2-css"/>
    <th:block th:include="common/include :: bootstrap-fileinput-css"/>
</head>
<body>
<div class="main-content">
    <form id="form-article-add" class="form-horizontal">
        <input name="catalogId" type="hidden" id="catalogId"/>
        <h4 class="form-header h4">基本信息</h4>
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-sm-2 control-label is-required">照片描述：</label>
                    <div class="col-sm-10">
                        <input name="desca" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">是否可以下载：</label>
                    <div class="col-sm-8">
                        <select name="isDown" class="form-control m-b" th:with="type=${@dict.getType('sys_yes_no')}"
                                required>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                    th:value="${dict.dictValue}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">是否展示：</label>
                    <div class="col-sm-8">
                        <select name="isRelease" class="form-control m-b" th:with="type=${@dict.getType('sys_yes_no')}"
                                required>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                    th:value="${dict.dictValue}"></option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">所属目录：</label>
                    <div class="col-sm-8">
                        <div class="input-group">
                            <input name="catalogName" onclick="selectCatalogTree()" id="catalogName" type="text"
                                   placeholder="请选择所属目录" class="form-control" required>
                            <span class="input-group-addon"><i class="fa fa-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">顺序：</label>
                    <div class="col-sm-8">
                        <input name="albumSort" class="form-control" type="number" required>
                    </div>
                </div>
            </div>
        </div>
        <h4 class="form-header h4"></h4>
        <div class="row">
            <label class="col-sm-3 control-label">图片：</label>
            <div class="col-sm-8">
                <input name="sourceUrl" class="form-control" type="hidden">
                <input id="fileUploads" name="file" type="file" class="imgFile form-control">
            </div>
        </div>
    </form>
</div>

<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存
        </button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭
        </button>
    </div>
</div>
<th:block th:include="common/include :: footer"/>
<th:block th:include="common/include :: select2-js"/>
<th:block th:include="common/include :: bootstrap-fileinput-js"/>
<script>
    var prefix = ctx + "content/album";
    var module = "album";

    $(function () {
        $('#fileUploads')
            .fileinput({
                language: 'zh',
                uploadUrl: ctx + 'common/upload',
                showCaption: true,
                showUpload: true,
                showRemove: true,
                showClose: true,
                browseClass: 'btn btn-primary',
                uploadExtraData: function (previewId, index) {
                    var data = {
                        module: module,
                        saveType: "dfs",
                        savePath: $("input[name='sourceUrl']").val()
                    };
                    return data;
                }
            })
            .on("change", function () {
                // 清除掉上次上传的图片
                $(".uploadPreview").find(".file-preview-frame:first").remove();
                $(".uploadPreview").find(".kv-zoom-cache:first").remove();
            })
            .on("filebatchselected", function (e, files) {
                $(this).fileinput("upload");             // 文件选择完直接调用上传方法。
            })
            .on("fileuploaded", function (e, data, previewiId, index) {       // 上传完成后的处理
                $("input[name='sourceUrl']").val(data.response.url);
            });
    });

    $("#form-article-add").validate({
        onkeyup: false,
        rules: {
            desca: {
                required: true
            },
            catalogName: {
                required: true
            },
            isDown: {
                required: true
            },
            isRelease: {
                required: true
            },
            albumSort: {
                required: true
            }
        },
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var data = $("#form-article-add").serializeArray();
            $.operate.saveTab(prefix + "/add", data);
        }
    }

    /*文章管理-新增-选择目录树*/
    function selectCatalogTree() {
        var treeId = $("#catalogId").val();
        var url = ctx + "content/catalog/selectCatalogTree/" + treeId + "-" + module;
        var options = {
            title: '选择目录',
            width: "380",
            url: url,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
        var tree = layero.find("iframe")[0].contentWindow.$._tree;
        if ($.tree.notAllowParents(tree)) {
            var body = layer.getChildFrame('body', index);
            $("#catalogId").val(body.find('#treeId').val());
            $("#catalogName").val(body.find('#treeName').val());
            layer.close(index);
        }
    }
</script>
</body>
</html>
